﻿using UnityEngine;
using System.Collections;

/// <summary>
/// util method.
/// </summary>
public static class wsUtil
{
	/// <summary>
	/// throw exception.
	/// </summary>
	/// <param name="message"></param>
	public static void Exception(string message)
	{
		throw new System.Exception(message);
	}

	/// <summary>
	/// do sleep.
	/// </summary>
	/// <param name="time"></param>
	public static void Sleep(int time)
	{
		System.Threading.Thread.Sleep(time);
	}

	/// <summary>
	/// limit the value between min and max.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="source"></param>
	/// <param name="min"></param>
	/// <param name="max"></param>
	/// <returns></returns>
	public static T LimitValue<T>(T source, T min, T max) where T : System.IComparable<T>
	{
		if (source.CompareTo(min) < 0)
			source = min;

		else if (source.CompareTo(max) > 0)
			source = max;

		return source;
	}

	/// <summary>
	/// limit the value between min and max.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="source"></param>
	/// <param name="min"></param>
	/// <param name="max"></param>
	public static void LimitValue<T>(ref T source, T min, T max) where T : System.IComparable<T>
	{
		source = LimitValue<T>(source, min, max);
	}

	/// <summary>
	/// copy position and rotation from transform.
	/// </summary>
	/// <param name="fromThis"></param>
	/// <param name="toThis"></param>
	public static void CopyTransform(Transform fromThis, Transform toThis)
	{
		toThis.position = fromThis.position;
		toThis.rotation = fromThis.rotation;
	}

	/// <summary>
	/// convert array to 2 dimentional array.
	/// </summary>
	/// <param name="source"></param>
	/// <param name="one"></param>
	/// <param name="two"></param>
	/// <returns></returns>
	public static int[,] ConvertArrayTo2DArray(int[] source, int one, int two)
	{
		if (source.Length != (one * two))
			return null;

		int[,] dest = new int[one, two];

		for (int i = 0; i < one; i++)
		{
			for (int m = 0; m < two; m++)
			{
				int idx = (i * two) + m;
				dest[i, m] = source[idx];
			}
		}

		return dest;
	}

	/// <summary>
	/// get color that added value.
	/// </summary>
	/// <param name="color"></param>
	/// <param name="addValue"></param>
	/// <returns></returns>
	public static Color GetAddedColor(Color color, float addValue)
	{
		color.a += addValue;
		LimitValue<float>(ref color.a, 0, 1);

		color.r += addValue;
		LimitValue<float>(ref color.r, 0, 1);

		color.g += addValue;
		LimitValue<float>(ref color.g, 0, 1);

		color.b += addValue;
		LimitValue<float>(ref color.b, 0, 1);

		return color;
	}
}
